package com.example.ibo.lollipopdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Created by admin on 16/1/12.
 */
public class VivzAdapter extends RecyclerView.Adapter<VivzAdapter.MyViewHolder> {

    private LayoutInflater layoutInflater;
    List<Informatoin> data = Collections.emptyList();
    private ClickListener clickListener;

    public VivzAdapter(Context context, List<Informatoin> data) {
        this.data = data;
        layoutInflater = LayoutInflater.from(context);
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.costom_row, parent, false);
        MyViewHolder holder = new MyViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Informatoin informatoin = data.get(position);

        holder.title.setText(informatoin.title);
        holder.icon.setImageResource(informatoin.resId);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnIteamClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void delete(int pos){
        data.remove(pos);
        notifyItemRemoved(pos);
    }

    public String getItem(int position) {
        return data.get(position).title;
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView icon;
        TextView title;

        public MyViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.listIcon);
            title = (TextView) itemView.findViewById(R.id.listMessage);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (clickListener != null) {
                clickListener.itemClicked(v, getPosition());
            }
        }
    }

    public interface ClickListener {
        public void itemClicked(View view, int position);
    }
}
